Skip to content

Fix: OpenGL init for StealthTip - #4959

Open
christopher-hampson wants to merge 2 commits into
ManimCommunity:mainfrom
christopher-hampson:fix_opengl_stealth_tip
Open

Fix: OpenGL init for StealthTip#4959
christopher-hampson wants to merge 2 commits into
ManimCommunity:mainfrom
christopher-hampson:fix_opengl_stealth_tip

Conversation

@christopher-hampson

Copy link
Copy Markdown
Contributor

Overview: What does this pull request change?

Fixes StealthTip initialization when using the OpenGL renderer.

Previous initialization bypassed ConvertToOpenGL metaclass, which results in the following error

AttributeError: 'StealthTip' object has no attribute 'data'

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

self.start_angle = start_angle
VMobject.__init__(
self, fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs
super(ArrowTip, self).__init__(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect there might be a nicer fix for this - happy to take any suggestions, this just seemed like the smallest patch that seemed to work.

@nikolajmunk

Copy link
Copy Markdown
Contributor

Here's an alternate suggestion: what if we made ArrowTip an abstract class? Then it would look like:

from abc import ABC, abstractmethod

class ArrowTip(VMobject, ABC, metaclass=ConvertToOpenGL):
    r"""Base class for arrow tips.

    .. seealso::
        :class:`ArrowTriangleTip`
        :class:`ArrowTriangleFilledTip`
        :class:`ArrowCircleTip`
        :class:`ArrowCircleFilledTip`
        :class:`ArrowSquareTip`
        :class:`ArrowSquareFilledTip`
        :class:`StealthTip`

    Examples
    --------
    Cannot be used directly, only intended for inheritance::

        >>> tip = ArrowTip()
        Traceback (most recent call last):
        ...
        NotImplementedError: Has to be implemented in inheriting subclasses.

    Instead, use one of the pre-defined ones, or make
    a custom one like this: ........
    """

    @abstractmethod
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
    
    # ... rest of implementation ...

This works well enough on my computer. If I try to render this scene with OpenGL, I get the expected image:

class ArrowTest(Scene):
    def construct(self):
        arrow = Arrow(tip_shape=StealthTip)
        self.add(arrow)

and if I instead render this scene with OpenGL...

class ArrowTest(Scene):
    def construct(self):
        arrow = Arrow(tip_shape=StealthTip)
        arrow2 = Arrow(tip_shape=ArrowTip)
        self.add(arrow)

... I get the following error:

172 │   │   color = self.get_color()                                                          │
│    173 │   │   style.update({"fill_color": color, "stroke_color": color})                        │
│    174 │   │   style.update(self.tip_style)                                                      │
│ ❱  175 │   │   tip = tip_shape(length=tip_length, **style)                                       │
│    176 │   │   return tip                                                                        │
│    177 │                                                                                         │
│    178def position_tip(self, tip: tips.ArrowTip, at_start: bool = False) -> tips.ArrowTip:  │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: Can't instantiate abstract class ArrowTip without an implementation for abstract methods '__init__', '_original__init__'

As far as I can tell, this doesn't break anything - at least not any tests - but then again there don't seem to be any non-graphical tests for Arrow or ArrowTip, so we never explicitly confirm that you are disallowed from instantiating ArrowTip.

Maybe this would be a good general approach to writing mobject mixins?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants